clear all;close all;clc

dur = 1;
% fsN = 44.1e3;
% load standard_hrir_database\subject_003\hrir_final.mat
[speech,fs0]=audioread('Speech_sound\BEL_SR\BEL_SR1913.wav');
fs = 32.1e3;
speech = resample(speech,fs,fs0);
speech = speech(1:round(dur*fs));
fs
figure;
nfft = round(40e-3.*fs) % window length for 80 ms
% % % % % Method 1=====
% % % % figure;
subplot(221)
spectrogram(speech,nfft,[],nfft,fs) % official
view([90 -90]);title('Spectrogram')
xlim([0 10])
spec_old=spectrogram(speech,nfft,[],nfft,fs); % official
'size'
size(spec_old)
% % % % subplot(222)
% % % % % contourf(abs(yy),'edgecolor','none')
% % % % contourf(abs([yy;flipud(yy)]),'edgecolor','none')
% % % % colormap('jet')
% % % % 
% % % % % sound(speech,fs)
% % % % [data1]=invspecgram([yy;flipud(yy)],nfft,fs,nfft,nfft/2);
% % % % % [data1]=invspecgram([yy],nfft,fs,nfft,nfft/2);
% % % % 
% % % % subplot(223)
% % % % plot(speech)
% % % % subplot(224)
% % % % plot(data1)
% % % % % pause(dur*1.5)
% % % % stop0


% Method 2====
        win = hamming(nfft,'periodic');

spec_new = stft(speech,fs,'Window',win,'FFTLength',nfft);
'size new'
size(spec_new)
subplot(222)
stft(speech,fs,'Window',win,'FFTLength',nfft);
ylim([-10 10]);title('stft')
re_speech=real(istft(spec_new,fs,'Window',win,'FFTLength',nfft));

figure;
contourf(abs(spec_new(end/2+1:end,:)));
set0

subplot(223)
plot(speech)
title('original sound')
subplot(224)
plot(re_speech);title('reconstructed sound')
sound(re_speech,fs)